package com.icarcom.cn.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.icarcom.cn.MainActivity;
import com.icarcom.cn.R;
import com.icarcom.cn.api.ACTION;
import com.icarcom.cn.api.HttpCallBack;
import com.icarcom.cn.api.HttpUtils;
import com.icarcom.cn.base.BaseActivity;
import com.icarcom.cn.ui.bean.LoginSuccessBean;
import com.icarcom.cn.util.DesCode;
import com.icarcom.cn.util.GsonUtil;
import com.icarcom.cn.util.L;
import com.icarcom.cn.util.SP;
import com.icarcom.cn.util.SpContent;
import com.icarcom.cn.util.T;
import com.lzy.okgo.cache.CacheMode;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements HttpCallBack {
    ProgressDialog progressDialog;
    String isLogin = "",timestamp = "",signc = "";
    @Bind(R.id.tv_login_submit)
    TextView tv_login_submit;
    @Bind(R.id.et_login_phone)
    EditText et_login_phone;
    @Bind(R.id.et_login_password)
    EditText et_login_password;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        super.initView();
        progressDialog = new ProgressDialog(this);
        isLogin = SP.get(this, SpContent.isLogin, "0") + "";
        if (isLogin.equals("1")) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
        tv_login_submit.setAlpha(0.5f);

        et_login_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
                if(!editable.toString().equals("")){
                    if(et_login_password.getText().toString().equals("")){
                        tv_login_submit.setAlpha(0.5f);
                    }else{
                        tv_login_submit.setAlpha(1.0f);
                    }
                }
            }
        });

        et_login_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
                if(!editable.toString().equals("")){
                    if(et_login_phone.getText().toString().equals("")){
                        tv_login_submit.setAlpha(0.5f);
                    }else{
                        tv_login_submit.setAlpha(1.0f);
                    }
                }
            }
        });
    }

    /**
     * 确认登录
     */
    private void loginSubmit() {

        long timeStampSec = System.currentTimeMillis()/1000;
        timestamp = String.format("%010d", timeStampSec);

        SortedMap<String,Object> params = new TreeMap<>();
        params.put("username",et_login_phone.getText().toString());
        params.put("password",DesCode.MD5(et_login_password.getText().toString()));
        params.put("timestamp",timestamp);
        params.put("signKey",SpContent.DesKey);

        Gson gsons = new Gson();
        String sourStrs = gsons.toJson(params);
        signc = DesCode.MD5(sourStrs);

        HashMap<String,String> param = new HashMap<>();
        param.put("username",et_login_phone.getText().toString());//后台管理员用户名
        param.put("password", DesCode.MD5(et_login_password.getText().toString()));//密码做MD5加密后转大写
        param.put("timestamp",timestamp);//时间戳，秒数
        param.put("sign",signc);//MD5签名后转为大写
        HttpUtils.doPost(this, ACTION.TESTLOGIN, param, CacheMode.REQUEST_FAILED_READ_CACHE, true, this);
    }

    @Override
    public void onSuccess(int action, String res) {
        switch (action){
            case ACTION.TESTLOGIN:
                L.e("???????????登录接口      "+res);
                LoginSuccessBean loginSuccessBean = GsonUtil.toObj(res,LoginSuccessBean.class);
                if(loginSuccessBean.getRetCode().equals("000000")){
                    SP.put(this,SpContent.userToken,loginSuccessBean.getToken());
                    SP.put(this,SpContent.isLogin,"1");
                    startActivity(new Intent(this, MainActivity.class));
                }else{
                    T.show(loginSuccessBean.getRetMsg());
                }
                break;
        }
    }

    @OnClick({R.id.tv_login_submit})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_login_submit:
                if(TextUtils.isEmpty(et_login_phone.getText().toString())){
                    T.show("请输入账号");
                }else if(TextUtils.isEmpty(et_login_password.getText().toString())){
                    T.show("请输入密码");
                }else{
                   loginSubmit();
                    //startActivity(new Intent(this,MainActivity.class));
                }
                break;
        }
    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void showErrorMessage(String s) {

    }
}
